<?php
/**
 * Created by PhpStorm.
 * User: dolia
 * Date: 2016/4/5
 * Time: 15:53
 */

namespace App\Http\Controllers\Api\V1;


use App\Exceptions\Api\ApiDoneErrorException;
use App\Exceptions\Api\ApiInputErrorException;
use App\Exceptions\Api\ApiRsaEecryptErrorException;
use App\Exceptions\Api\ApiUserNotLoginErrorException;
use App\Exceptions\Api\ApiValidateErrorException;
use App\Http\Controllers\Controller;
use App\Interfaces\AdService;
use App\Interfaces\CategoryService;
use App\Interfaces\BrandService;
use App\Interfaces\GoodsService;
use Illuminate\Http\Request;

class CatController extends Controller{

    /**
     * @api {post} /cat/get_tree 获取所有分类
     * @apiName Cat/getTree
     * @apiGroup Cat
     * @apiDescription 获取所有分类
     * @apiSuccess {Object[]}  Object  分类树信息
     * @apiSuccess {int}  Object.cat_id   分类ID
     * @apiSuccess {int}  Object.cat_name   分类名称
     * @apiSuccess {string}  Object.keywords   分类关键词
     * @apiSuccess {string}  Object.cat_desc   分类描述
     * @apiSuccess {string}  Object.parent_id   父ID
     * @apiSuccess {int}  Object.sort_order   排序
     * @apiSuccess {int}  Object.measure_unit   计量单位
     * @apiSuccess {int}  Object.goods_type   分类搜索属性
     * @apiSuccess {int}  Object.tariff_percent   关税比例
     * @apiSuccess {int}  Object.is_recommend   是否推荐
     * @apiSuccess {object[]}  Object.children   二级分类信息
     * @apiSuccess {int}  Object.children.cat_id   分类ID
     * @apiSuccess {string}  Object.children.cat_name   分类名称
     * @apiSuccess {string}  Object.children.keywords   分类关键词
     * @apiSuccess {string}  Object.children.cat_desc   分类描述
     * @apiSuccess {int}  Object.children.parent_id   父ID
     * @apiSuccess {int}  Object.children.sort_order   排序
     * @apiSuccess {string}  Object.children.measure_unit   计量单位
     * @apiSuccess {int}  Object.children.goods_type   分类搜索属性
     * @apiSuccess {int}  Object.children.tariff_percent   关税比例
     * @apiSuccess {int}  Object.children.is_recommend   是否推荐
     * @apiSuccess {int}  Object.children.cat_picture   分类展示图
     * @apiSuccess {int}  Object.children.mobile_cat_picture   分类展示图地址
     * @apiSuccess {object[]}  Object.children.children   三级分类信息
     * @apiSuccess {int}  Object.children.children.cat_id   分类ID
     * @apiSuccess {string}  Object.children.children.cat_name   分类名称
     * @apiSuccess {string}  Object.children.children.keywords   分类关键词
     * @apiSuccess {string}  Object.children.children.cat_desc   分类描述
     * @apiSuccess {int}  Object.children.children.parent_id   父ID
     * @apiSuccess {int}  Object.children.children.sort_order   排序
     * @apiSuccess {string}  Object.children.children.measure_unit   计量单位
     * @apiSuccess {int}  Object.children.children.goods_type   分类搜索属性
     * @apiSuccess {int}  Object.children.children.tariff_percent   关税比例
     * @apiSuccess {int}  Object.children.children.is_recommend   是否推荐
     * @apiSuccess {string}  Object.children.children.cat_picture   分类展示图
     * @apiSuccess {string}  Object.children.children.mobile_cat_picture   分类展示图地址
     * @apiSuccessExample {json} Success-Response:
     * HTTP/1.1 200 OK
     [
        {
        "cat_id": 6,
        "cat_name": "营养保健",
        "keywords": "营养保健",
        "cat_desc": "保健品",
        "parent_id": 0,
        "sort_order": 3,
        "measure_unit": "",
        "goods_type": 0,
        "tariff_percent": 10,
        "is_recommend": 1,
        "children": [
            {
                "cat_id": 451,
                "cat_name": "功能保健品",
                "keywords": "保健,保养",
                "cat_desc": "2级分类功能保健品",
                "parent_id": 6,
                "sort_order": 100,
                "measure_unit": "包",
                "goods_type": 0,
                "tariff_percent": 10,
                "is_recommend": 1,
                "children": [
                    {
                    "cat_id": 455,
                    "cat_name": "美容养颜",
                    "keywords": "美容,养颜",
                    "cat_desc": "3级分类美容养颜",
                    "parent_id": 451,
                    "sort_order": 100,
                    "measure_unit": "包",
                    "goods_type": 0,
                    "tariff_percent": 10,
                    "is_recommend": 1,
                    "cat_picture": null,
                    "children": []，
                    "mobile_cat_picture": null
                    }
                ]
            },
            {
                "cat_id": 456,
                "cat_name": "营养保健品",
                "keywords": "保健,营养",
                "cat_desc": "2级分类营养保健品",
                "parent_id": 6,
                "sort_order": 100,
                "measure_unit": "包",
                "goods_type": 0,
                "tariff_percent": 10,
                "is_recommend": 1,
                "children": [
                    {
                    "cat_id": 458,
                    "cat_name": "蛋白质",
                    "keywords": "保健,影响,蛋白质",
                    "cat_desc": "3级分类蛋白质",
                    "parent_id": 456,
                    "sort_order": 100,
                    "measure_unit": "包",
                    "goods_type": 0,
                    "tariff_percent": 10,
                    "is_recommend": 1,
                    "cat_picture": null,
                    "children": []，
                    "mobile_cat_picture": null
                    }
            }
            ]
            }
        ]
        }
     ]
     * @apiErrorExample Error-Response 01:
     * HTTP/1.1 202 Accepted
    {
    "message": "没有任何分类"
    }
     */
    public function getTree(CategoryService $categoryService){
        $tree = $categoryService->getTree();
        if($tree){
            return response()->json($tree);
        }else{
            throw new ApiDoneErrorException('没有任何分类');
        }
    }

    /**
     * @api {post} /cat/get_top 获取顶级分类
     * @apiName Cat/getTop
     * @apiGroup Cat
     * @apiDescription 获取顶级分类
     * @apiSuccess {Object[]}  Object  顶级分类信息
     * @apiSuccess {int}  Object.cat_id   分类ID
     * @apiSuccess {string}  Object.cat_name   分类名称
     * @apiSuccess {string}  Object.keywords   分类关键词
     * @apiSuccess {string}  Object.cat_desc   分类描述
     * @apiSuccess {int}  Object.parent_id   父ID
     * @apiSuccess {int}  Object.sort_order   排序
     * @apiSuccess {string}  Object.measure_unit   计量单位
     * @apiSuccess {int}  Object.goods_type   分类搜索属性
     * @apiSuccess {string}  Object.tariff_percent   关税比例
     * @apiSuccess {int}  Object.is_recommend   是否推荐
     * @apiSuccess {string} Object.cat_picture 分类展示图
     * @apiSuccess {string} Object.mobile_cat_picture 分类展示图地址
     * @apiSuccessExample {json} Success-Response:
     * HTTP/1.1 200 OK
    [
    {
    "cat_id": 3,
    "cat_name": "美妆个护",
    "keywords": "美容彩妆",
    "cat_desc": "美妆洗护、个人护理",
    "parent_id": 0,
    "sort_order": 7,
    "measure_unit": "",
    "goods_type": 0,
    "tariff_percent": 10,
    "is_recommend": 1
    },
    {
    "cat_id": 9,
    "cat_name": "进口美食",
    "keywords": "饮料、酒水、茶叶",
    "cat_desc": "饮料、酒水、茶叶",
    "parent_id": 0,
    "sort_order": 6,
    "measure_unit": "",
    "goods_type": 0,
    "tariff_percent": 10,
    "is_recommend": 1
    "cat_picture": null,
    "mobile_cat_picture": null
    }
    ]
     * @apiErrorExample Error-Response 01:
     * HTTP/1.1 202 Accepted
    {
    "message": "没有顶级分类"
    }
     */
    public function getTop(CategoryService $categoryService){
        $top = $categoryService->top();
        if($top){
            return response()->json($top);
        }else{
            throw new ApiDoneErrorException('没有顶级分类');
        }
    }



    /**
     * @api {post} /cat/get_child/{cat_id} 获取子分类
     * @apiName Cat/getChild
     * @apiGroup Cat
     * @apiDescription 获取子分类
     * @apiSuccess {Object[]} Object 子分类信息
     * @apiSuccess {int} Object.cat_id 分类ID
     * @apiSuccess {string} Object.cat_name 分类名称
     * @apiSuccess {string} Object.keywords 分类关键词
     * @apiSuccess {string} Object.cat_desc 分类描述
     * @apiSuccess {int} Object.parent_id 父ID
     * @apiSuccess {int} Object.sort_order 排序
     * @apiSuccess {string} Object.measure_unit 计量单位
     * @apiSuccess {int} Object.goods_type 分类搜索属性
     * @apiSuccess {int} Object.tariff_percent 关税比例
     * @apiSuccess {int} Object.is_recommend 否是隐藏：0：隐藏；1：显示
     * @apiSuccess {string} Object.cat_picture 分类展示图
     * @apiSuccess {string} Object.mobile_cat_picture 分类展示图地址
     * @apiSuccessExample {json} Success-Response:
     * HTTP/1.1 200 OK
    [
        {
        "cat_id": 397,
        "cat_name": "香水/香氛",
        "keywords": "香水,香氛",
        "cat_desc": "2级分类香水/香氛",
        "parent_id": 3,
        "sort_order": 6,
        "measure_unit": "盒",
        "goods_type": 0,
        "tariff_percent": 50,
        "is_recommend": 1，
        "cat_picture": null,
        "mobile_cat_picture": null
        }
    ]
     * @apiErrorExample Error-Response 01:
     * HTTP/1.1 400 Bad Request
    {
    "message": "缺少参数cat_id"
    }
     * @apiErrorExample Error-Response 02:
     * HTTP/1.1 202 Accepted
    {
    "message": "该分类不存在"
    }
     */
    public function getChild($cat_id,CategoryService $categoryService){
        if(empty($cat_id)){
            throw new ApiInputErrorException('缺少参数cat_id');
        }
        $child = $categoryService->getChild($cat_id);
        if($child){
            return response()->json($child);
        }else{
            throw new ApiDoneErrorException('该分类不存在');
        }
    }

    /**
     * @api {post} /cat/get/{cat_id} 根据分类ID获取分类
     * @apiName Cat/getByCatId
     * @apiGroup Cat
     * @apiDescription 根据分类ID获取分类
     * @apiSuccess {Object[]} Object 分类信息
     * @apiSuccess {int} Object.cat_id 分类ID
     * @apiSuccess {string} Object.cat_name 分类名称
     * @apiSuccess {string} Object.keywords 分类关键词
     * @apiSuccess {string} Object.cat_desc 分类描述
     * @apiSuccess {int} Object.parent_id 父ID
     * @apiSuccess {int} Object.sort_order 排序
     * @apiSuccess {string} Object.measure_unit 计量单位
     * @apiSuccess {int} Object.goods_type 分类搜索属性
     * @apiSuccess {int} Object.tariff_percent 关税比例
     * @apiSuccess {int} Object.is_recommend 否是隐藏：0：隐藏；1：显示
     * @apiSuccess {string} Object.cat_picture 分类展示图
     * @apiSuccess {string} Object.mobile_cat_picture 分类展示图地址
     * @apiSuccessExample {json} Success-Response:
     * HTTP/1.1 200 OK
    {
        "cat_id": 3,
        "cat_name": "美妆个护",
        "keywords": "美容彩妆",
        "cat_desc": "美妆洗护、个人护理",
        "parent_id": 0,
        "sort_order": 7,
        "measure_unit": "",
        "goods_type": 0,
        "tariff_percent": 10,
        "is_recommend": 1
        "cat_picture": null,
        "mobile_cat_picture": null
    }
     * @apiErrorExample Error-Response 01:
     * HTTP/1.1 400 Bad Request
    {
    "message": "缺少参数cat_id"
    }
     * @apiErrorExample Error-Response 02:
     * HTTP/1.1 202 Accepted
    {
    "message": "该分类下没有子分类"
    }
     */
    public function getByCatId($cat_id,CategoryService $categoryService){
        if(empty($cat_id)){
            throw new ApiInputErrorException('缺少参数cat_id');
        }
        $child = $categoryService->getByCatId($cat_id);
        if($child){
            return response()->json($child);
        }else{
            throw new ApiDoneErrorException('该分类下没有子分类');
        }

    }

    /**
     * @api {post} /cat/get_attribute/{cat_id} 根据分类ID获取分类属性信息
     * @apiName Cat/getCategoryAttribute
     * @apiGroup Cat
     * @apiDescription 根据分类ID获取分类属性信息
     * @apiSuccess {Object[]} Object 属性信息
     * @apiSuccess {int} Object.id 属性ID
     * @apiSuccess {string} Object.name 属性名称
     * @apiSuccess {Object[]} Object.attribute_value 属性值信息
     * @apiSuccess {int} Object.attribute_value.id 属性值ID
     * @apiSuccess {int} Object.attribute_value.category_attribute_id 属性ID
     * @apiSuccess {string} Object.attribute_value.attribute_value 属性值
     * @apiSuccessExample {json} Success-Response:
     * HTTP/1.1 200 OK
    [
        {
            "id": 11,
            "attribute_name": "大小",
            "attribute_value": [
                {
                "id": 32,
                "category_attribute_id": 11,
                "attribute_value": "L"
                }
            ]
        },
        {
        "id": 7,
        "attribute_name": "颜色",
        "attribute_value": []
        },
        {
        "id": 44,
        "attribute_name": "图案",
        "attribute_value": []
        }
    ]
     * @apiErrorExample Error-Response 01:
     * HTTP/1.1 400 Bad Request
    {
    "message": "缺少参数cat_id"
    }
     * @apiErrorExample Error-Response 02:
     * HTTP/1.1 202 Accepted
    {
    "message": "该分类下没属性"
    }
     */
    public function getCategoryAttribute($cat_id,CategoryService $categoryService){
        if(empty($cat_id)){
            throw new ApiInputErrorException('缺少参数cat_id');
        }
        $categoryAttribute = $categoryService->getCategoryAttribute($cat_id);
        if($categoryAttribute){
            return response()->json($categoryAttribute);
        }else{
            throw new ApiDoneErrorException('该分类下没属性');
        }
    }

} 